import { Cloud } from '@tbmp/mp-cloud-sdk';
const cloud = new Cloud();
import { getSystemInfo } from './utils/tools'

// 云应用初始化
cloud.init({
  env: 'online',
});
let that = App({
  cloud,
  async onLaunch(options) {
  
    // 第一次打开
    if (options && options.query && options.query.channel) {
      let channel = options.query.channel;
      that.globalData.channel = channel
    }
    if (options && options.query && options.query.active) {
      let active = options.query.active;
      that.globalData.active = active
    }

    let res = await getSystemInfo()
    console.log('-----> systemInfo: ', res)
    // my.alert({ content: res.model });
    that.globalData.systemInfo = res
    that.globalData.isX = res.isX;
    that.globalData.platform = res.platform;
    console.log(that.globalData.platform, 'that.globalData.platform')

    let str = '0123456789abcdef';
    that.globalData.traceId = '';
    for (let i = 0; i < 20; i++) {
      let idx = parseInt(Math.random() * 16);
      that.globalData.traceId += str[idx];
    };

  },
  onShow(options) {
  },
  globalData: {
    traceId: "",
    platform: "",
    active: 1,
    channel: "taobao",
    channel2: "taobao",
    BaseUrl: 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2024Chanel_n5_drop/',
    commonUrl: 'https://morefun-active.oss-cn-beijing.aliyuncs.com/2021AJ13/',
    imgData: {
      column1: null,
      column2: null,
      column3: null,
      column4: null,
      column5: null,
      column6: null,
      column7: null,
      x: 0,
      y: 0,
      isChecLength: 0,
      isSet_x_y: true
    },
    textData: {
      selectedItems: null,
      checkTextList: null,
    },
    typeData: {
      num: 5,
    },
    pageState: false,
    pageNum: 0,
  },

  async track(customType, type = '', value = '') {
    var info = {
      version: "v3",
      traceId: that.globalData.traceId,
      projectCode: "2699",
      timestamp: new Date().getTime().toString(),
      device: that.globalData.platform,
      province: "",
      city: "",
      customType: customType,
      type: type,
      platform: "taobao",
      channel: that.globalData.channel,
      value,
    }
    let result = await cloud.function.invoke('morefun', { data: info }, 'addTrack');
    console.log("result", result);
  },

  async trackMoreFun(customType, type = '', value = '') {
    try {
      const openId = await that.getUserInfo()
      let p = await getSystemInfo()
      console.log(p,'openIdopenIdopenIdopenId',that.globalData)

      var info = {
        version: "v4",
        traceId: that.globalData.traceId,
        projectCode: "2728",
        timestamp: new Date().getTime().toString(),
        device: p.platform,
        province: "",
        city: "",
        customType: customType,
        type: type,
        platform: "taobao",
        channel: that.globalData.channel2 || that.globalData.channel,
        value,
        page: '二级页',
        userId: openId

      }
      let result = await cloud.function.invoke('morefun', { data: info }, 'addTrack2');
      console.log("result", result);
    } catch (error) {
      console.log(error, 'trackMoreFun')
    }
  },
  async getUserInfo() {
    // 在本地缓存中获取,直接返回
    let openId = '';
    const userInfo = my.getStorageSync({ key: 'webview-openId' });
    if (userInfo && userInfo.data) openId = userInfo.data;
    if (!openId) {
      const res = await cloud.function.invoke('fathersday');
      if (res && res.success && res.data && res.data.openId) {
        openId = res.data.openId;
        my.setStorageSync({
          key: 'webview-openId',
          data: openId
        });
      }
    }
    return openId;
  },
  // 弱提示
  showToast(content) {
    my.hideToast()
    my.showToast({
      content,
      duration: 1500,
    });
  },
});
